﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'region';
</cfscript>

<cffunction name="getregion" access="remote" returnFormat="json">
<cfparam name="rank" default="0" type="integer">
  <cfscript>
	j = {"success":"0"};
	child = arraynew(1);
	</cfscript>
  <cfif isdefined('name')>
  	<cfswitch expression="#rank#">
    	<cfcase value="0">
      	<cfinvoke method="getProvinceCode" returnvariable="getProvinceCode" region="#name#" />
				<cfif getProvinceCode.recordcount gt 0>
        	<cfinvoke method="getcity" returnvariable="getcity" codes="#getProvinceCode.codes#" />
          <cfoutput query="getcity">
          	<cfif region eq '市辖区' or region eq ' 县'>
              <cfinvoke component="app.cfc.region" method="getdistrict" returnvariable="getdistrict" codes="#codes#" />
            	<cfloop query="getdistrict">
              	<cfscript>
								arrayappend(child,region);
								</cfscript>
              </cfloop>
            <cfelse>
							<cfscript>
              arrayappend(child,region);
              </cfscript>
            </cfif>
          </cfoutput>
          
        </cfif>
        
      </cfcase>
    	<cfcase value="1">
      	<cfinvoke method="getCityCode" returnvariable="getCityCode" region="#name#" />
				<cfif getCityCode.recordcount gt 0>
          <cfinvoke component="app.cfc.region" method="getdistrict" returnvariable="getdistrict" codes="#getCityCode.codes#" />
          <cfloop query="getdistrict">
            <cfscript>
						if (region neq '市辖区'){
							arrayappend(child,region);
						}
            </cfscript>
          </cfloop>
        </cfif>
      </cfcase>
    </cfswitch>
  <cfelse>
  	
    <cfinvoke component="app.cfc.region" method="getProvince" returnvariable="getProvince" />
    <cfoutput query="getProvince">
      <cfscript>
        arrayappend(child,region);
      </cfscript>
    </cfoutput>
    
  
  </cfif>
  



  <cfscript>
	j = {"success":"1","child":child};
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>
</cffunction>




<!---获取省编码--->
<cffunction name="getProvinceCode" access="public" returntype="query">
<cfquery datasource="#DB#" name="getProvinceCode">
SELECT *
FROM #dbt#
WHERE codes like '%0000' AND region like '#region#%'
</cfquery>
<cfreturn getProvinceCode>
</cffunction>

<!---获取市编码--->
<cffunction name="getCityCode" access="public" returntype="query">
<cfquery datasource="#DB#" name="getCityCode">
SELECT *
FROM #dbt#
WHERE codes like '%00' AND region = '#region#'
</cfquery>
<cfreturn getCityCode>
</cffunction>

<!---获取省--->
<cffunction name="getProvince" access="public" returntype="query">
<cfquery datasource="#DB#" name="getProvince">
SELECT *
FROM #dbt#
WHERE
 codes like '%0000'
</cfquery>
<cfreturn getProvince>
</cffunction>


<!---获取市--->
<cffunction name="getcity" access="public" returntype="query">
<cfquery datasource="#DB#" name="getcity">
SELECT *
FROM #dbt#
WHERE 
codes like '#left(codes,3)#%' AND codes <> '#codes#' AND codes like '%00'
</cfquery>
<cfreturn getcity>
</cffunction>

<!---获取区县--->
<cffunction name="getdistrict" access="public" returntype="query">
<cfquery datasource="#DB#" name="getdistrict">
SELECT *
FROM #dbt#
WHERE 
codes like '#left(codes,4)#%' AND codes <> '#codes#'
</cfquery>
<cfreturn getdistrict>
</cffunction>


</cfcomponent>